package com.shg.admin.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Slf4j
//@WebServlet(urlPatterns = {"/my"})
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        // TODO-解决原生Servlet响应乱码问题
        
        // 下面这种方式不能解决
        //resp.setCharacterEncoding("utf-8");
        //resp.getWriter().write("我是原生的Servlet响应的请求");

        // 下面这种方式不能解决
        //String responseValue = "我是原生的Servlet响应的请求";
        //byte[] responseValueBytes = responseValue.getBytes(StandardCharsets.ISO_8859_1);
        //responseValue = new String(responseValueBytes, StandardCharsets.UTF_8);
        //resp.getWriter().write(responseValue);

        //resp.getWriter().write("我是原生的Servlet响应的请求");

        // 这种方式可以解决
        //resp.setContentType("text/plain;charset=utf-8");
        resp.getWriter().write("我是原生的Servlet响应的请求");
    }
}
